package com.example.noteshare;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import com.example.noteshare.Utilities.Constants;
import com.example.noteshare.Utilities.Repo;
import com.example.noteshare.asyncTasks.AsyncPost;
import com.example.noteshare.gson.AccountCreationResult;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AccountCreationPage extends Activity implements OnClickListener {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_account_creation_page);
		Button btnLogin = (Button) this.findViewById(R.id.button1_create);
		btnLogin.setOnClickListener(this);
	}

	public void onClick(View v){
		// Get account info
		EditText nameID = (EditText) findViewById(R.id.enter_name);
		EditText emailID = (EditText) findViewById(R.id.enter_email);
		EditText bdayID = (EditText) findViewById(R.id.enter_bday);
		EditText userid = (EditText) findViewById(R.id.input_name);
		EditText password = (EditText) findViewById(R.id.enter_password);
		EditText password2 = (EditText) findViewById(R.id.reenter_password);
		
		// Set info to strings
		String name =  nameID.getText().toString();
		String email = emailID.getText().toString();
		String birthday = bdayID.getText().toString();
		String username =  userid.getText().toString();
		String pass = password.getText().toString();
		String pass2 = password2.getText().toString();
		
		
		
		
		boolean go = true;
		
		String[] nameParts = name.split(" ");
		String[] birthdayParts = birthday.split("\\/");
		
		if(nameParts.length < 2){
			go = false;
		}
		
		if(birthdayParts.length < 3){
			go = false;
		}
		
		if(!pass.equals(pass2)){
			go = false;
		}
		
		if(go){
			Map<String, String> params = new HashMap<String, String>();
			params.put("username", username);
			params.put("password", pass);
			params.put("email", email);
			params.put("firstName", nameParts[0]);
			params.put("lastName", nameParts[1]);
			params.put("birthDay", birthdayParts[1]);
			params.put("birthMonth", birthdayParts[0]);
			params.put("birthYear", birthdayParts[2]);			
			
			//create the async task
			AsyncPost async = new AsyncPost(Constants.serverIP + Constants.accountCreation, params);
			
			String result = "{}";
			try {
				result = async.execute().get();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
			
			//parse tje results
			Gson gson = new GsonBuilder().create();
			AccountCreationResult acr = gson.fromJson(result, AccountCreationResult.class);
			
			boolean successful;			
			try{
				Repo.userId = acr.getId();
				successful = Boolean.parseBoolean(acr.getSuccessful());
			}catch(Exception e){
				successful = false;
			}
			
			if(successful){
				// Start intent
				Intent i = new Intent(AccountCreationPage.this, MenuPage.class);
				startActivity(i);
			}else{
				Toast.makeText(getApplicationContext(), "Failed to create account", Toast.LENGTH_LONG).show();
			}
			
		}else{
			Toast.makeText(getApplicationContext(), "Failed to create account", Toast.LENGTH_LONG).show();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.account_creation_page, menu);
		return true;
	}

}
